package com.ruoyi.eam.service.impl;

import java.util.List;

import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamChgH;
import com.ruoyi.eam.doman.EamStockD;
import com.ruoyi.eam.mapper.EamAssetMapper;
import com.ruoyi.eam.mapper.EamChgHMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.mapper.EamChgDMapper;
import com.ruoyi.eam.doman.EamChgD;
import com.ruoyi.eam.service.IEamChgDService;

/**
 * 资产变更从Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-01
 */
@Service
public class EamChgDServiceImpl implements IEamChgDService
{
    @Autowired
    private EamChgDMapper eamChgDMapper;
    @Autowired
    private EamChgHMapper chgHMapper;
    @Autowired
    private EamAssetMapper eamAssetMapper;

    /**
     * 查询资产变更从
     *
     * @param alterationId 资产变更从主键
     * @return 资产变更从
     */
    @Override
    public EamChgD selectEamChgDByAlterationId(Long alterationId)
    {
        return eamChgDMapper.selectEamChgDByAlterationId(alterationId);
    }

    /**
     * 查询资产变更从列表
     *
     * @param eamChgD 资产变更从
     * @return 资产变更从
     */
    @Override
    public List<EamChgD> selectEamChgDList(EamChgD eamChgD)
    {
        return eamChgDMapper.selectEamChgDList(eamChgD);
    }

    /**
     * 新增资产变更从
     *
     * @param eamChgD 资产变更从
     * @return 结果
     */
    @Override
    public int insertEamChgD(List<EamAsset> eamAssets)
    {
        //获取最后一条变更id
        Long chgIdLast = chgHMapper.queryEamChgHList().get(chgHMapper.queryEamChgHList().size() - 1).getAlterationId();
        for (EamAsset asset : eamAssets) {
            EamChgD eamChgD = new EamChgD();
            eamChgD.setAlterationId(chgIdLast);
            eamChgD.setAssetId(asset.getAssetId());
            eamChgD.setCount(asset.getAssetNum());
            //添加变更从表信息
            int num = eamChgDMapper.insertEamChgD(eamChgD);
            //修改资产信息
            int num1 = eamAssetMapper.updateEamAsset(asset);
            if(num == 0 || num1 == 0){
                return 0;
            }
        }
        return 1;
    }

    /**
     * 修改资产变更从
     *
     * @param eamChgD 资产变更从
     * @return 结果
     */
    @Override
    public int updateEamChgD(EamChgD eamChgD)
    {
        return eamChgDMapper.updateEamChgD(eamChgD);
    }

    /**
     * 批量删除资产变更从
     *
     * @param alterationIds 需要删除的资产变更从主键
     * @return 结果
     */
    @Override
    public int deleteEamChgDByAlterationIds(Long[] alterationIds)
    {
        return eamChgDMapper.deleteEamChgDByAlterationIds(alterationIds);
    }

    /**
     * 删除资产变更从信息
     *
     * @param alterationId 资产变更从主键
     * @return 结果
     */
    @Override
    public int deleteEamChgDByAlterationId(Long alterationId)
    {
        return eamChgDMapper.deleteEamChgDByAlterationId(alterationId);
    }

    @Override
    public int insertChgD(List<EamAsset> eamAssets,Long alterationId) {
        for (EamAsset asset : eamAssets) {
            EamChgD eamChgD = new EamChgD();
            eamChgD.setAlterationId(alterationId);
            eamChgD.setAssetId(asset.getAssetId());
            eamChgD.setCount(asset.getAssetNum());
            //添加变更从表信息
            int num = eamChgDMapper.insertEamChgD(eamChgD);
            //修改资产信息
            int num1 = eamAssetMapper.updateEamAsset(asset);
            if(num == 0 || num1 == 0){
                return 0;
            }
        }
        return 1;
    }
}
